

#ifndef __COMM_FILE_H__
#define __COMM_FILE_H__

#include "comm/comm_base.h"
#include <iostream>
#include <list>
#include <vector>

using namespace std;

/**************************************************************************************
功能描述: 项目文件管理
修改记录:
**************************************************************************************/
class FILE_MNG_C
{
public:
    FILE_MNG_C(void)
    {
#ifdef WIN32
        strMapFileDir_       = "";
        strTraceFileDir_     = "";
        strRouteFileDir_     = "";
        strCfgFileDir_       = "";
        strBoundaryFileDir_  = "";
#else
        string strUsrName;
        if (0 != GetLoginUserName(strUsrName)) {
            ST_LOG_ERR("GetLoginUserName() Fail.");
            strUsrName = "zhishan";
        }
        
        strMapFileDir_       = "/home/" + strUsrName + "/map_config/";
        strTraceFileDir_     = "/home/" + strUsrName + "/map_config/";
        strRouteFileDir_     = "/home/" + strUsrName + "/map_config/";
        strCfgFileDir_       = "/home/" + strUsrName + "/map_config/";
        strBoundaryFileDir_  = "/home/" + strUsrName + "/map_config/";
        strLidarConfigDir_   = "/home/" + strUsrName + "/map_config/lidar_config/";
        strCameraConfigDir_  = "/home/" + strUsrName + "/map_config/camera_param/";
        strVxlFilterFileDir_ = "/home/" + strUsrName + "/map_config/";
        
        strWorkSceneDir_     = "/home/" + strUsrName + "/map_config/work_scene/";
        strLocationMapDir_   = "/home/" + strUsrName + "/map_config/work_scene/location_map/";

        strLogFileDir_       = "/home/" + strUsrName + "/map_config/log/processprint/";
#endif
    }

    void GetMapDir(string &strMapDir);
    void GetTraceFileDir(string &strFileDir);
    void GetRouteFileDir(string &strFileDir);
    void GetCfgFileDir(string &strFileDir);
    void GetBoundaryFileDir(string &strFileDir);
    void GetLidarConfigDir(string &strFileDir);
    void GetCameraConfigDir(string &strFileDir);
    void GetVxlFilterFileDir(string &strFileDir);
 
    void GetWorkSceneDir(string &strDir) const;
    void GetLocationMapDir(string &strDir) const;

    void GetLogFileDir(string &strDir) const;
    
private:
    string strMapFileDir_;
    string strTraceFileDir_;
    string strRouteFileDir_;
    string strCfgFileDir_;
    string strBoundaryFileDir_;
    string strLidarConfigDir_;
    string strCameraConfigDir_;
    string strVxlFilterFileDir_;

    string strWorkSceneDir_;
    string strLocationMapDir_;

    string strLogFileDir_;
    
    INT32 GetLoginUserName(string &strUserName);
};

class FILE_OPRT_C
{
public:
    bool IsExist(const char *pcFilePath);
    bool IsExist(const string &strFilePath);
    int  Rename(const string &strOldFilePath, const string &strNewFilePath, bool isCoverFile);
    void DelFile(const string &strFilePath);
    int  CreateDirFile(const string &strDirPath);
    void DelDirFile(const string &strFilePath);
    int  Copy(const string &strSrcFilePath, const string &strTgtFilePath, bool isCoverFile);
    void SyncBuffToFile(FILE *pf);
    void SyncFileToDisk(FILE *pf);

    void GetFileNameFrmPath(const string &strFilePath, string &strFileName);
    void GetFileDirFrmPath(const string &strFilePath, string &strFileDir);
private:
};

/**************************************************************************************
功能描述: 地图文件管理
修改记录:
**************************************************************************************/
class MAP_FILE_MNG_C
{
public:
    MAP_FILE_MNG_C(void)
    {
        objFileMng_.GetMapDir(strMapDir_);
    }

    MAP_FILE_MNG_C(const string &strMapDir)
    {
        strMapDir_ = strMapDir;
    }
    
    void GetMapDir(string &strMapDir);
    void GetMapPath(const string &strMapName, string &strMapPath);
    void GetMapFilePath(const string &strMapFileName, string &strMapFilePath);

    void GetCombMapFileName(string &strCombMapFileName);
    void GetCombMapFilePath(string &strCombMapFilePath);
    void GetSplitCfgFileName(const string &strSrcMapName, string &strSplitCfgFileName);
    void GetSplitCfgFilePath(const string &strSrcMapName, string &strSplitCfgFilePath);

    void GetMapExYamlFilePath(string &strMapExYamlFilePath);
    
    void  GetSubMapName(UINT32 subMapNo, string &strSubMapName);
    INT32 GetSubMapNo(string &strSubMapName);
    void  GetSubMapPath(UINT32 subMapNo, string &strSubMapPath);

    void GetDefaultMapName(string &strDefaultMapName);
    void GetDefaultYamlFileName(string &strDefaultYamlFileName);
    void GetDefaultYamlFilePath(string &strDefaultYamlFilePath);
    void GetDefaultPgmFileName(string &strDefaultPgmFileName);
    void GetDefaultPgmFilePath(string &strDefaultPgmFilePath);

    void GetYamlFileName(const string &strMapName, string &strYamlFileName);
    void GetYamlFilePath(const string &strMapName, string &strYamlFilePath);
    void GetPgmFileName(const string &strMapName, string &strPgmFileName);
    void GetPgmFilePath(const string &strMapName, string &strPgmFilePath);

    void GetTmpMapName(const string &strMapName, string &strTmpMapName);
    void GetTmpYamlFileName(const string &strMapName, string &strTmpYamlFileName);
    void GetTmpYamlFilePath(const string &strMapName, string &strTmpYamlFilePath);
    void GetTmpPgmFileName(const string &strMapName, string &strTmpPgmFileName);
    void GetTmpPgmFilePath(const string &strMapName, string &strTmpPgmFilePath);

    void GetVirtualObstMapName(UINT32 subMapNo, string &strVirtualObstMapName);
    void GetVirtualObstMapPath(UINT32 subMapNo, string &strVirtualObstMapPath);

    void GetSimulaitonObstMapPath(string &strObstMapPath);

    bool isMapPathExist(const string &strMapPath);

    INT32 RenameMap(const string &strSrcMapName, const string &strTgtMapName);
    INT32 CopyMap(const string &strSrcMapName, const string &strTgtMapName);
    void  DelMap(const string &strMapName);
    
private:
    FILE_MNG_C  objFileMng_;
    FILE_OPRT_C objFileOprt_;

    string strMapDir_;
    
    const string strDefautMapName_               = "zhishanmap";
    const string strTmpFileExName_               = "_tmp";
    const string strVirtualObstMapExName_        = "_virtual_obst";
    const string strSimulationObstMapExName_     = "_simulation_obst";
    const string strMapYamlSuffix_               = ".yaml";
    const string strMapPgmSuffix_                = ".pgm";
    const string strCombMapFileName_             = "comb_map.xml";
    const string strSplitCfgFileExName_          = "_split_cfg.xml";
    const string strMapExYamlFileName_           = "zhishanmapEx.yaml";

    INT32 RenamePgmFile(const string &strSrcMapName, const string &strTgtMapName);
    INT32 RenameYamlFile(const string &strSrcMapName, const string &strTgtMapName);
    INT32 CopyPgmFile(const string &strSrcMapName, const string &strTgtMapName);
    INT32 CopyYamlFile(const string &strSrcMapName, const string &strTgtMapName);
    void  DelPgmFile(const string &strMapName);
    void  DelYamlFile(const string &strMapName);
};

class WORK_SCENE_FILE_MNG_C
{
public:
    WORK_SCENE_FILE_MNG_C(void)
    {
        FILE_MNG_C objFileMng;
        objFileMng.GetWorkSceneDir(strWorkSceneDir_);

        strNavigationMapDir_        = strWorkSceneDir_ + "navigation_map/";
        strLocationMapDir_          = strWorkSceneDir_ + "location_map/";
        strSimulationMapDir_        = strWorkSceneDir_ + "simulation_map/";

        strRoadsInfoFileName_       = "map_roads.xml";
        strSimulationMapName_       = "simulation_obst";
    }

    void GetRoadsInfoFilePath(string &strPath) const;
    void GetRoadRouteFilePath(UINT32 roadNo, UINT32 routeId, string &strFilePath) const;
    void GetRoadLineFilePath(UINT32 roadNo, UINT32 lineId, string &strFilePath) const;
    
    void GetSimulationObstMapPath(string &strPath) const;

    const string &GetLocMapDir(void) const;
    const string &GetNavMapDir(void) const;
    const string &GetSimulationMapDir(void) const;
    
private:
    string strWorkSceneDir_;
    string strNavigationMapDir_;
    string strLocationMapDir_;
    string strSimulationMapDir_;
    
    string strRoadsInfoFileName_;
    string strSimulationMapName_;
};

/**************************************************************************************
功能描述: 配置文件管理
修改记录:
**************************************************************************************/
class CFG_FILE_MNG_C
{
public:
    CFG_FILE_MNG_C(void)
    {
        FILE_MNG_C objFileMng;
        objFileMng.GetCfgFileDir(strCfgFileDir_);

        strCfgParamFileName_ = "cfg_param.xml";
    }
    
    void GetCfgParamFilePath(string &strCfgParamFilePath);
    
private:
    string strCfgFileDir_;
    string strCfgParamFileName_;
};

#endif

